﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Net;


namespace FreeOPC.Config.Model
{
    public class OPCServerDTO
    {
    //    "DAProgId":"1",
    //"AEProgId":"2",
    //"OPCServerIP":"3",
    //"SubsystemTypeId":2
        public string DAProgId { get; set; }
        public string AEProgId { get; set; }
        public string OPCServerIP { get; set; }
        public int SubsystemTypeId { get; set; }
    }


    public class OPCServerModel
    {
        private const string AE_PREFIX = "opcae://";

        private const string DA_PREFIX = "opcda://";

        public OPCServerModel()
        {
            CallbackCycle = 5000;
            CallbackCapacity = 500;
            //Credential = new NetworkCredential("Administrator", "admin", "10.156.2.102");
        }

        #region data memebers
        /// <summary>
        /// Opc Server Id
        /// </summary>
        [DataMember(Name = "Id")]
        public Int32 _Id;
        public System.Int32? Id { get { return this._Id; } set { this._Id = value ?? default(System.Int32); } }

        /// <summary>
        /// Opc Server Name
        /// </summary>
        [DataMember(Name = "Name")]
        public string Name { get; set; }

        /// <summary>
        /// Opc Server Machine IP
        /// </summary>
        [DataMember(Name = "OPCServerIP")]
        public string OPCServerIP { get; set; }

        /// <summary>
        /// DA Prog Id
        /// </summary>
        [DataMember(Name = "DAProgId")]
        public string DAProgId { get; set; }

        /// <summary>
        /// AE Prog Id
        /// </summary>
        [DataMember(Name = "AEProgId")]
        public string AEProgId { get; set; }

        /// <summary>
        /// SubsystemTypeId
        /// </summary>
        [DataMember(Name = "SubsystemTypeId")]
        public int SubsystemTypeId { get; set; }


        public bool IsDisable { get; set; }
        public string OPCServerManufacturer { get; set; }
        public DateTime Updatetime { get; set; }
        public int ReconnectTime { get; set; }
        public int ReconnectIntervalTime { get; set; }




        /// <summary>
        /// SubsystemTypeName
        /// </summary>
        [DataMember(Name = "SubsystemTypeName")]
        public string SubsystemTypeName { get; set; }

        /// <summary>
        /// SubsystemTypeDisplayName
        /// </summary>
        [DataMember(Name = "SubsystemTypeDisplayName")]
        public string SubsystemTypeDisplayName { get; set; }


        [DataMember(Name = "PlantName")]
        public string PlantName { get; set; }

        /// <summary>
        /// Opc Server Connection Status
        /// </summary>
        [DataMember(Name = "ConnectSuccess")]
        public bool ConnectSuccess { get; set; }

        /// <summary>
        /// Display Name
        /// </summary>
        [DataMember(Name = "DisplayName")]
        public string DisplayName { get; set; }

        private List<FCSStation> _stations = new List<FCSStation>();
        [DataMember(Name = "Stations")]
        public List<FCSStation> Stations
        {
            get
            {
                return _stations;
            }
            set
            {
                _stations = value;
            }
        }

        private int m_OPCBufferTime = 1000;
        private int m_OPCEvtsBufferSize = 0;
        private int m_OPCKeepAlive = 0;// ms
        private int m_numDaysHistory = 1;// set initial value as 1 which means AE client should receive today's data.

        public int NumDaysHistory
        {
            get
            {
                return m_numDaysHistory;
            }
        }

        public int OPCBufferTime
        {
            get
            {
                return m_OPCBufferTime;
            }
        }


        public int OPCEvtsBufferSize
        {
            get
            {
                return m_OPCEvtsBufferSize;
            }
        }
        public int OPCKeepAlive
        {
            get
            {
                return m_OPCKeepAlive;
            }
        }

        public int CallbackCycle { get; set; }

        public int CallbackCapacity { get; set; }

        #endregion

        //public NetworkCredential Credential { get; set; }

        /// <summary>
        /// Gets OPC AE Server URL
        /// </summary>
        /// <returns></returns>
        public string GetAEServerUrl()
        {
            //return "opcae://" + OPCServerIP + "/" + AEProgId + "/" + AEClassId;
            //return "opcae://" + OPCServerIP + "/" + AEProgId;
            return UpdateURL(AE_PREFIX, OPCServerIP, AEProgId);
        }

        /// <summary>
        /// Gets OPC DA Server URL
        /// </summary>
        /// <returns></returns>
        public string GetDAServerUrl()
        {
            //return "opcda://" + OPCServerIP + "/" + DAProgId + "/" + DAClassId;
            //return "opcda://" + OPCServerIP + "/" + DAProgId;
            return UpdateURL(DA_PREFIX, OPCServerIP, DAProgId);
        }

        public string UpdateURL(string url_prefix, string hostName, string progID)
        {

            const string LOCALHOST_NAME = "localhost";
            string tmpUrl = url_prefix + hostName + "/" + progID;
            if (hostName.ToLower() == LOCALHOST_NAME || hostName == "")
            {
                tmpUrl = url_prefix + "/" + progID;
            }
            return tmpUrl;
        }
    }
}
